﻿/*--------------------------------------------------------------------------------
 * Adaptive Intelligence - Telnet UI
 * Version 1.00
 * 
 * Application Options Dialog Class
 *      Provides a dialog definition for displaying and modifying the application options.
 *------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------
 * System Imports
 *------------------------------------------------------------------------------*/
using System;
using System.Drawing;
using System.Windows.Forms;

using Adaptive.Foundation.Windows.Forms;
using Adaptive.Telnet.UI.Properties;

namespace Adaptive.Telnet.UI
{
    /// <summary>
    /// Provides a dialog definition for displaying and modifying the application options.
    /// </summary>
    public partial class ApplicationOptionsDialog : ModalDialogBase 
    {
        /*--------------------------------------------------------------------------------
         * Private Member Declarations
         *------------------------------------------------------------------------------*/
        #region Private Member Declarations
        /// <summary>
        /// Settings instance.
        /// </summary>
        private Settings _applicationSettings;

        #endregion

        /*--------------------------------------------------------------------------------
         * Constructor / Dispose / Destructor Methods
         *------------------------------------------------------------------------------*/
        #region Constructor / Dispose / Destructor Methods
        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        /// <remarks>
        /// This is the default constructor.
        /// </remarks>
        public ApplicationOptionsDialog()
        {
            InitializeComponent();
            _applicationSettings = new Settings();
        }
        /// <summary>
        /// Deallocates internally allocated objects.
        /// </summary>
        /// <param name="disposing">
        /// A value indicating whether the object is being disposed.
        /// </param>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                    components.Dispose();
            }

            _applicationSettings = null;
            components = null;
            base.Dispose(disposing);
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Protected Methods / Event Methods
         *------------------------------------------------------------------------------*/
        #region Protected Methods / Event Methods
        /// <summary>
        /// Creates and assigns the event handlers for the controls on the dialog.
        /// </summary>
        protected override void AssignEventHandlers()
        {
            BottomPanel.OkClick +=new EventHandler(HandleOkClick);
            BottomPanel.CancelClick +=new EventHandler(HandleCancelClick);
            SelectFontButton.Click +=new EventHandler(HandleSelectFontClick);
            SaveAsButton.Click +=new EventHandler(HandleSaveAsClick);
        }
        /// <summary>
        /// Removes the event handlers for the controls on the dialog.
        /// </summary>
        protected override void RemoveEventHandlers()
        {
            BottomPanel.OkClick -= new EventHandler(HandleOkClick);
            BottomPanel.CancelClick -= new EventHandler(HandleCancelClick);
            SelectFontButton.Click -= new EventHandler(HandleSelectFontClick);
            SaveAsButton.Click -= new EventHandler(HandleSaveAsClick);
        }
        /// <summary>
        /// Sets the initial content for the dialog.
        /// </summary>
        protected override void SetInitialContent()
        {
            FontStyle style = FontStyle.Regular;

            FileNameLabel.Text = _applicationSettings.ConnectionFile;
            FontSampleText.Text = _applicationSettings.DefaultFontName;

            if (_applicationSettings.DefaultFontBold)
                style |= FontStyle.Bold;
            if (_applicationSettings.DefaultFontItalic)
                style |= FontStyle.Italic;
            if (_applicationSettings.DefaultFontUnderline)
                style |= FontStyle.Underline;

            FontSampleText.Font = new Font(_applicationSettings.DefaultFontName, (float)_applicationSettings.DefaultFontSize, style);
            FontSampleText.Text = _applicationSettings.DefaultFontName;
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Public Methods / Functions
         *------------------------------------------------------------------------------*/
        #region Public Methods / Functions
        /// <summary>
        /// Saves the application options values.
        /// </summary>
        public void SaveOptions()
        {
            _applicationSettings.ConnectionFile = FileNameLabel.Text;
            _applicationSettings.DefaultFontName = FontSampleText.Font.Name;
            _applicationSettings.DefaultFontBold = FontSampleText.Font.Bold;
            _applicationSettings.DefaultFontItalic = FontSampleText.Font.Italic;
            _applicationSettings.DefaultFontUnderline = FontSampleText.Font.Underline;
            _applicationSettings.DefaultFontSize = FontSampleText.Font.Size;

            _applicationSettings.Save();
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Private Event Handlers
         *------------------------------------------------------------------------------*/
        #region Private Event Handlers
        /// <summary>
        /// Handles the event when the OK button is clicked.
        /// </summary>
        /// <param name="sender">The object raising the event.</param>
        /// <param name="e">An <see cref="EventArgs"/> event arguments instance.</param>
        private void HandleOkClick(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;
            DialogResult = DialogResult.OK;
            Close();
        }
        /// <summary>
        /// Handles the event when the Cancel button is clicked.
        /// </summary>
        /// <param name="sender">The object raising the event.</param>
        /// <param name="e">An <see cref="EventArgs"/> event arguments instance.</param>
        private void HandleCancelClick(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;
            DialogResult = DialogResult.Cancel;
            Close();
        }
        /// <summary>
        /// Handles the event when the Select Font... button is clicked.
        /// </summary>
        /// <param name="sender">The object raising the event.</param>
        /// <param name="e">An <see cref="EventArgs"/> event arguments instance.</param>
        private void HandleSelectFontClick(object sender, EventArgs e)
        {
            FontSelectionDialog dialog = null;              //Dialog to be displayed.
            DialogResult result = DialogResult.Cancel;      //Result of interaction.

            Cursor = Cursors.WaitCursor;
            dialog = new FontSelectionDialog();
            result = dialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                FontSampleText.Font = dialog.SelectedFont;
                FontSampleText.Text = dialog.SelectedFont.Name;
            }
            dialog.Dispose();
            dialog = null;
            Cursor = Cursors.Default;
        }
        /// <summary>
        /// Handles the event when the Save as... button is clicked.
        /// </summary>
        /// <param name="sender">The object raising the event.</param>
        /// <param name="e">An <see cref="EventArgs"/> event arguments instance.</param>
        private void HandleSaveAsClick(object sender, EventArgs e)
        {
            SaveFileDialog dialog = null;                   //Dialog to be displayed.
            DialogResult result = DialogResult.Cancel;      //Result of user interaction.

            Cursor = Cursors.WaitCursor;
            dialog = new SaveFileDialog();
            dialog.AddExtension = true;
            dialog.AutoUpgradeEnabled = true;
            dialog.CheckPathExists = true;
            dialog.DefaultExt = "definitions";
            dialog.Filter = "Definitions File (*.definitions)|*.definitions|All Files(*.*)|*.*";
            dialog.InitialDirectory = System.IO.Path.GetDirectoryName(FileNameLabel.Text);
            dialog.OverwritePrompt = false;
            dialog.Title = "Save Connections Data As";
            result = dialog.ShowDialog();

            if (result == DialogResult.OK)
            {
                FileNameLabel.Text = dialog.FileName;
            }
            dialog.Dispose();
            dialog = null;
            Cursor = Cursors.Default;

        }
        #endregion
    }
}